package com.hjq.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.hjq.mapper.EmpMapper;
import com.hjq.mapper.StudentMapper;
import com.hjq.pojo.PageResult;
import com.hjq.pojo.Student;
import com.hjq.pojo.queryparam.StudentQueryParam;
import com.hjq.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
@Service
public class StudentServiceImpl implements StudentService {
    @Autowired
    StudentMapper studentMapper;
    @Autowired
    private EmpMapper empMapper;

    @Override
    public PageResult<Student> findByPage(StudentQueryParam studentQueryParam) {
        Page<Object> page = PageHelper.startPage(studentQueryParam.getPage(), studentQueryParam.getPageSize());
        List<Student> studentList = studentMapper.findByPage(studentQueryParam);
        long total = page.getTotal();
        return new PageResult<>(total, studentList);

    }

    @Override
    public void insert(Student student) {
        student.setCreateTime(LocalDateTime.now());
        student.setUpdateTime(LocalDateTime.now());
        student.setViolationCount((short) 0);
        student.setViolationScore((short) 0);
        studentMapper.insert(student);
    }

    @Override
    public Student findById(Integer id) {
        Student student= studentMapper.findById(id);
        return student;
    }

    @Override
    public void update(Student student) {
        student.setUpdateTime(LocalDateTime.now());
        studentMapper.update(student);
    }

    @Override
    public void delete(List<Integer> ids) {
        studentMapper.delete(ids);
    }

    @Override
    public void handleViolation(Integer id, Integer score) {
        studentMapper.handleViolation(id,score,LocalDateTime.now());
    }
}
